#include "GOIIndex.h"
#include "../std.h"
#include "../Widgets/Colours.h"
#include "../Widgets/UIBuilder.h"
#include "../Utilities/Convert.h"
#include "../Utilities/trim.h"

GOIIndex::GOIIndex(){
		_getData = false;
		mProcessor = new GOIProcessor();
		mLayout = createMainLayout(getStringNotIterative(L_SERVICE), getStringNotIterative(L_UPDATE), getStringNotIterative(L_SELECT), getStringNotIterative(L_EXIT));
		mItems = (ListBox*)mLayout->getChildren()[1];
		mSoftkey = (Widget*)mLayout->getChildren()[2];
		this->setMain(mLayout);
}

GOIIndex::~GOIIndex(){
	delete mLayout;
}

void GOIIndex::keyPressEvent(int keyCode, int nativeCode)
{
	switch(keyCode)
	{
		case MAK_UP:
		case MAK_2:
			mItems->selectPreviousItem();
			break;
		case MAK_DOWN:
		case MAK_8:
			mItems->selectNextItem();
			break;
		case MAK_SOFTRIGHT:
		case MAK_6:
			APPCONTROLLER->abortRegister();
			APPCONTROLLER->close();
			break;
		case MAK_FIRE:{
			LRESItemWidget* widget = (LRESItemWidget*)mItems->getChildren()[mItems->getSelectedIndex()];
			MessageBox* msg = (MessageBox*) APPCONTROLLER->getScreen("MessageBox");
			msg->setListener(this);
			String t = getStringIterative(L_MSG_SENDSMS) + widget->getShortCode();
			msg->setTitle(t);
			msg->show();
		}
			break;
		case MAK_SOFTLEFT:
			mItems->setSelectedIndex(0);
			mItems->clear();
			Label* l = (Label*)mSoftkey->getChildren()[1];
			l->setCaption("Loading");
			l->update();
			mProcessor->addRequest(APPCONTROLLER->getUrl(), this);
			break;
	}
}

void GOIIndex::actionIndex(int index){
	LRESItemWidget* widget = (LRESItemWidget*)mItems->getChildren()[mItems->getSelectedIndex()];
	if (index == MAK_SOFTLEFT){
		int result = maSendTextSMS(widget->getShortCode().c_str(), widget->getSmsCnt().c_str());
		if (result >= 0){
			APPCONTROLLER->show("MainScreen");
		}
	}else{
		this->show();
	}
}

void GOIIndex::itemReceived(GOIItem& item){
	LRESItemWidget* newItem = new LRESItemWidget(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTITEM, mItems);

	newItem->setCompanyName(item.name);
	newItem->setCompanyFont(APPCONTROLLER->getFont("default"));
	newItem->setDrawDate(item.description);
	newItem->setDrawDateFont(APPCONTROLLER->getFont("default"));

	newItem->setSmsCnt(item.keyword);
	newItem->setShortCode(item.serviceid);

	newItem->setSkin(APPCONTROLLER->getSkin("default"));
	//newItem->addWidgetListener(this);

	mItems->setSelectedIndex(0);
	mItems->getChildren()[0]->setSelected(true);
}

void GOIIndex::triggered(Widget* sender)
{
	//Act as if fire has been pressed
	//keyPressEvent(MAK_FIRE, 0);
}

void GOIIndex::pointerReleaseEvent(MAPoint2d point){

}

void GOIIndex::pointerPressEvent(MAPoint2d point){
	Point p;
	p.set(point.x, point.y);

	if (mItems->contains(p)){
		for(int i=0; i< mItems->getChildren().size(); i++){
			if (mItems->getChildren()[i]->contains(p)){
				int index = mItems->getSelectedIndex();
				if (index == i) keyPressEvent(MAK_FIRE, 0);
				else mItems->setSelectedIndex(i);
				break;
			}
		}
	}
	else if (mSoftkey->contains(p)){
		if(mSoftkey->getChildren()[0]->contains(p)) keyPressEvent(MAK_SOFTLEFT, 0);
		else if (mSoftkey->getChildren()[2]->contains(p)) keyPressEvent(MAK_SOFTRIGHT, 0);
	}
}

void GOIIndex::processingComplete(){
	_getData  = true;
	Label* l = (Label*)mSoftkey->getChildren()[1];
	l->setCaption(getStringIterative(L_SELECT));
	l->update();
	lprintfln("process GOI complete");
}

void GOIIndex::show(){
	if (!_getData){
		Label* l = (Label*)mSoftkey->getChildren()[1];
		l->setCaption("Loading");
		l->update();
		mProcessor->addRequest(APPCONTROLLER->getUrl(), this);
	}/*else if (mItems->getChildren()[0] != NULL){
		mItems->setSelectedIndex(0);
		mItems->getChildren()[0]->setSelected(true);
	}*/
	Screen::show();
}
